/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.sgf.dt.provider.utils;

import com.inspur.edp.lcm.metadata.api.entity.GspMetadata;
import com.inspur.edp.lcm.metadata.api.entity.MetadataProject;
import com.inspur.edp.lcm.metadata.api.entity.uri.MetadataURI;
import com.inspur.edp.lcm.metadata.api.service.MetadataProjectService;
import com.inspur.edp.lcm.metadata.api.service.MetadataService;
import com.inspur.edp.lcm.metadata.api.service.RefCommonService;
import com.inspur.edp.sgf.dt.provider.exception.EApiDtVOExceptionCode;
import com.inspur.edp.sgf.utils.exception.EApiDesignException;
import io.iec.edp.caf.businessobject.api.entity.DevBasicBoInfo;
import io.iec.edp.caf.businessobject.api.service.DevBasicInfoService;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

/**
 * @author Fynn Qi
 * @author Kaixuan Shi
 * @version v1.1
 * @since 2020/9/1 14:15
 */
public class MetadataUtils {

    public static GspMetadata getDtVoMetadata(MetadataURI targetMdURI, MetadataURI sourceMdURI, String projectPath) {
        if (targetMdURI == null || StringUtils.isBlank(targetMdURI.getId())) {
            throw new EApiDesignException(EApiDtVOExceptionCode.GSP_EAPI_PROVIDER_0101, "外部服务获取解析的资源ID为空");
        }
        if (targetMdURI.getId().contains("##")) {
            String[] temps = targetMdURI.getId().split("##");
            MetadataService service = SpringBeanUtils.getBean(MetadataService.class);
            return service.getRefMetadata(temps[1], temps[0]);
        } else {
            RefCommonService service = SpringBeanUtils.getBean(RefCommonService.class);
            return service.getRefMetadata(targetMdURI, sourceMdURI, projectPath);
        }
    }

    public static MetadataProject getMetadataProject(String path) {
        MetadataProjectService service = SpringBeanUtils.getBean(MetadataProjectService.class);
        if (service == null) {
            throw new EApiDesignException(EApiDtVOExceptionCode.GSP_EAPI_PROVIDER_0102,
                    "获取MetadataProjectService Bean为空");
        }
        MetadataProject projectInfo = service.getMetadataProjInfo(path);
        if (projectInfo == null) {
            throw new EApiDesignException(EApiDtVOExceptionCode.GSP_EAPI_PROVIDER_0103,
                    String.format("根据相对路径【%s】获取元数据工程信息为空！", path));
        }
        return projectInfo;
    }

//    public static GspProject getProjectInfo(GspMetadata metadata) {
//        if (StringUtils.isBlank(metadata.getRelativePath())) {
//            throw new EApiDesignException(EApiDtVOExceptionCode,
//                    String.format(
//                            "元数据【%s(ID:%s)】的相对路径为空，无法获取工程信息！",
//                            metadata.getHeader().getCode(), metadata.getHeader().getId()));
//        }
//        GspProjectService service = SpringBeanUtils.getBean(GspProjectService.class);
//        if (service == null) {
//            throw new EApiDesignException(EApiDtVOExceptionCode,"获取MetadataProjectService Bean为空");
//        }
//        GspProject projectInfo = null;
//        try {
//            projectInfo = service.getGspProjectInfo(metadata.getRelativePath());
//        } catch (IOException e) {
//            throw new EApiDesignException(EApiDtVOExceptionCode,e);
//        }
//        if (projectInfo == null) {
//            throw new EApiDesignException(EApiDtVOExceptionCode,
//                    String.format(
//                            "元数据【%s(ID:%s)】根据相对路径【%s】获取工程信息为空！",
//                            metadata.getHeader().getCode(),
//                            metadata.getHeader().getId(),
//                            metadata.getRelativePath()));
//        }
//        return projectInfo;
//    }

    public static DevBasicBoInfo getDevBasicBoInfo(String bizId) {
        DevBasicInfoService devBasicInfoService = SpringBeanUtils.getBean(DevBasicInfoService.class);
        DevBasicBoInfo devBasicBoInfo = devBasicInfoService.getDevBasicBoInfo(bizId);
        Assert.notNull(devBasicBoInfo, String.format("根据业务对象ID[%s]获取的DevBasicBoInfo为空", bizId));
        if (StringUtils.isBlank(devBasicBoInfo.getAppCode())) {
            throw new EApiDesignException(EApiDtVOExceptionCode.GSP_EAPI_PROVIDER_0104,
                    String.format("根据业务对象ID[%s]获取的DevBasicBoInfo为的AppCode为空", bizId));
        }
        if (StringUtils.isBlank(devBasicBoInfo.getSuCode())) {
            throw new EApiDesignException(EApiDtVOExceptionCode.GSP_EAPI_PROVIDER_0105,
                    String.format("根据业务对象ID[%s]获取的DevBasicBoInfo为的SuCode为空", bizId));
        }
        return devBasicBoInfo;
    }
}
